package cn.wizzer.app.web.commons.ext.websocket.action.live;

import cn.wizzer.app.user.modules.models.User;
import cn.wizzer.app.user.modules.models.constant.ChatConstant;
import cn.wizzer.app.web.commons.ext.websocket.main.action.BaseNettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.action.NettyAction;
import cn.wizzer.app.web.commons.utils.NettyMsgUtil;
import cn.wizzer.app.web.commons.utils.RedisUtil;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.util.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

/**
 * @author 22099
 */
@IocBean
@NettyAction(uri = "/joinRoom")
public class JoinAction extends BaseNettyAction {

    @Inject
    private RedisService redisService;

    public static BaseNettyAction instance() {
        return BaseNettyAction.instance(JoinAction.class);
    }

    @Override
    public Result action(Channel income, JSONObject reqParams) {
        //用户信息
        String userStr = RedisUtil.hget(ChatConstant.CHAT_USER, income.id().toString());
        User user = JSONUtil.parseStringToJavaBean(User.class, userStr);
        String originRoom = RedisUtil.hget(User.class, ChatConstant.CHAT_USER, income.id().toString()).getRoomId();
        //要加入的聊天室
        JSONObject params = JSONUtil.toJSON(reqParams);
        String roomId = params.getString("roomId");
        boolean n = NettyMsgUtil.joinRoom(income, user, roomId) ?
                //lremove 房间中移除用户信息
                NettyMsgUtil.leaveRoom(income, originRoom) : false;

        return n?Result.success():Result.error();
    }

    @Override
    public Result subscribe(String subscribeParam) {
        return Result.success();
    }

}
